package com.wn.ticket.service.impl;

import com.wn.ticket.entity.UserAddress;
import com.wn.ticket.exception.UserAddressExistException;
import com.wn.ticket.mapper.UserAddressMapper;
import com.wn.ticket.mapper.UserPerformMapper;
import com.wn.ticket.service.UserAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 杜昭进
 * date: 2022/9/2
 */
@Slf4j
@Service
public class UserAddressServiceImpl implements UserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    /**
     * 新增（总数5条，有且仅有一个默认地址）
     * 事务控制
     */
    @Transactional
    @Override
    public void add(UserAddress userAddress) {
        List<UserAddress> sa = userAddressMapper.selectByUserId(userAddress.getUser().getId());
        log.debug("-----------sa:{}",sa);
        boolean temp = false;// 判断是否有默认
        if (sa.size() >= 5) {
            throw new UserAddressExistException("地址栏已满");
        }
        if (userAddress.getIsDefault() != null && userAddress.getIsDefault().equals("y")) {
            for (int i = 0; i < sa.size(); i++) {
                if (sa.get(i).getIsDefault().equals("y")) {
                    sa.get(i).setIsDefault("n");
                    userAddressMapper.update(sa.get(i));// 调方法操作数据库
                    temp = true;
                    break;
                }
            }
            temp = true;
        } else {
            userAddress.setIsDefault("n");
            temp = true;
        }
        if (temp) {
            userAddressMapper.insert(userAddress);
        }
    }

    /**
     * 修改
     */
    @Override
    public void update(UserAddress userAddress) {
        List<UserAddress> sa = userAddressMapper.selectByUserId(userAddress.getUser().getId());
        boolean temp = false;// 判断是否有默认
        if (userAddress.getIsDefault() != null && userAddress.getIsDefault().equals("y")) {
            for (int i = 0; i < sa.size(); i++) {
                if (sa.get(i).getIsDefault().equals("y")) {
                    sa.get(i).setIsDefault("n");
                    userAddressMapper.update(sa.get(i));// 调方法操作数据库
                    temp = true;
                    break;
                }
            }
            temp = true;
        } else {
            temp = true;
        }
        if (temp) {
            userAddressMapper.update(userAddress);
        }
    }

    /**
     * 删除（不能删除默认）
     * 物理删除
     */
    @Override
    public void delete(UserAddress userAddress) {
        UserAddress sh = userAddressMapper.selectById(userAddress.getId());
        if (sh.getIsDefault().equals("y")) {
            throw new UserAddressExistException("不能删除默认地址");
        }
        userAddressMapper.delete(userAddress.getId(), userAddress.getUser().getId());
    }

    /**
     * 查询地址
     */
    @Override
    public UserAddress seleteOne(int id) {
        return userAddressMapper.selectById(id);
    }

    /**
     *
     * 根据用户ID查询地址
     */
    @Override
    public List<UserAddress> seleteByUserId(int userId) {
        List<UserAddress> sh = userAddressMapper.selectByUserId(userId);
        return sh;
    }

    /**
     * 查询所有地址
     */
    @Override
    public List<UserAddress> seleteAll() {
        List<UserAddress> sh = userAddressMapper.selectAll();
        return sh;
    }
}
